﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using EOSCRM.Util ;
using EOSCRM.Domain;
using EOSCRM.Web.Common;
using EOSCRM.Dao;
using EOSCRM.Web.Shared;
using EOSCRM.Web.UserControls;


namespace EOSCRM.Web.Forms.DanhMuc
{
    public partial class CapNhatGia : Authentication
    {
        private readonly ConfigDao _objDao = new ConfigDao();
       

        
        private Mode UpdateMode
        {
            get
            {
                try
                {
                    if (Session[SessionKey.MODE] != null)
                    {
                        var mode = Convert.ToInt32(Session[SessionKey.MODE]);
                        return (mode == Mode.Update.GetHashCode()) ? Mode.Update : Mode.Create;
                    }

                    return Mode.Create;
                }
                catch (Exception)
                {
                    return Mode.Create;
                }
            }
            set
            {
                Session[SessionKey.MODE] = value.GetHashCode();
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    BindGia();
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }


        protected void BindGia()
        {
            var list = _objDao.GetListStartWith("DonGia");
            foreach (var item in list)
            {
                if (item.Name.Equals(Constants.ConfigDonGia))
                    txtGiaThuHoach.Text = item.Value;
                else if (item.Name.Equals(Constants.ConfigDonGiaThu))
                    txtGiaMuaVao.Text = item.Value;
                else txtGiaBanRa.Text = item.Value;
            }
            upnlInfor.Update();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var gia = ConvertUtil.ToDecimal(txtGiaThuHoach.Text.Trim());
            if (!gia.HasValue)
            {
                ShowError("Giá thu hoạch không hợp lệ", txtGiaThuHoach.ClientID);
                CloseWaitingDialog();
                return;
            }
            var giamua = ConvertUtil.ToDecimal(txtGiaMuaVao.Text.Trim());
            if (!giamua.HasValue)
            {
                ShowError("Giá thu mua không hợp lệ", txtGiaMuaVao.ClientID);
                CloseWaitingDialog();
                return;
            }
            var giaban = ConvertUtil.ToDecimal(txtGiaBanRa.Text.Trim());
            if (!giaban.HasValue)
            {
                ShowError("Giá bán không hợp lệ", txtGiaBanRa.ClientID);
                CloseWaitingDialog();
                return;
            }
            Message msg;

            // insert new
            if (!HasUpdatePermission)
            {
                CloseWaitingDialog();
                ShowError(Resources.Message.WARN_PERMISSION_DENIED);
                return;
            }
            msg = _objDao.UpdateGia(txtGiaThuHoach.Text.Trim(),txtGiaMuaVao.Text.Trim(),txtGiaBanRa.Text.Trim());

            CloseWaitingDialog();

            if (msg == null) return;

            if (msg.MsgType != MessageType.Error)
            {
                ShowInfor(ResourceLabel.Get(msg));
                BindGia();
            }
            else
            {
                ShowError(ResourceLabel.Get(msg));
            }
        }

        
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            BindGia();
            CloseWaitingDialog();
        }

    }
}